<template>
  <div class="redemptionCodeList">
    <h3>兑换码列表</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入标题" v-model="search1">
          <template slot="prepend">标题</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入兑换码" v-model="search2">
          <template slot="prepend">兑换码</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>状态</label>
        <el-select v-model="search3" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="cond2 cond">
        <label>是否过期</label>
        <el-select v-model="search4" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button
        type="success"
        icon="el-icon-plus"
        @click="addData"
        v-if="permissions_obj.id_107||permissions_obj.id_all"
      >添加</el-button>
      <el-button type="danger" icon="el-icon-download" @click="exportFile">导出</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="标题" width="200"></el-table-column>
        <el-table-column header-align="center" align="center" prop="code" label="兑换码" width="150"></el-table-column>
        <!-- <el-table-column
          header-align="center"
          align="center"
          prop="type_text"
          label="类型"
          width="150"
        ></el-table-column> -->
        <el-table-column header-align="center" align="center" prop="day" label="会员天数" width="100"></el-table-column>
        <el-table-column header-align="center" align="center" prop="limit" label="限制次数" width="100"></el-table-column>
        <el-table-column header-align="center" align="center" prop="expire" label="有效期" width="100"></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="备注"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="状态"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="130" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="permissions_obj.id_109||permissions_obj.id_all"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //添加广告信息弹窗 -->
    <el-dialog title="创建兑换码" :visible.sync="creatCodeDialogVisible" width="40%" center>
      <div class="row">
        <span>兑换码标题：</span>
        <el-input v-model="inputAdTitle" placeholder="请输入兑换码标题" clearable></el-input>
      </div>
      <div class="row">
        <span>会员天数：</span>
        <el-input v-model="inputDayNum" placeholder="请输入兑换会员时长" clearable></el-input>
      </div>
      <div class="row">
        <span>限制次数：</span>
        <el-input-number v-model="inputTimes" :min="1" ></el-input-number>
      </div>
      <div class="row">
        <span>有效期：</span>
        <el-date-picker
          v-model="inputEffeDayNum"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
      <div class="row">
        <span>生成数量：</span>
        <el-input v-model="inputCreatedNum" placeholder="请输入生成数量" clearable></el-input>
      </div>
      <!-- <div class="row">
        <span>类型：</span>
        <el-select v-model="inputType" placeholder="请选择">
          <el-option
            v-for="item in optionsType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div> -->
      <div class="row">
        <span>备注：</span>
        <el-input v-model="inputNote" placeholder="请输入备注" clearable type="textarea"></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitCreatCod">提交</el-button>
        <el-button type="info" size="small" @click="cancelCreatCod">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "redemptionCodeList",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      search4: "",
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "已使用"
        },
        {
          value: 1,
          label: "未使用"
        }
      ],
      options2: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "未过期"
        },
        {
          value: 1,
          label: "已过期"
        }
      ],
      optionsType: [
        {
          value: 1,
          label: "多次使用"
        },
        {
          value: 2,
          label: "单次使用"
        }
      ],
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      creatCodeDialogVisible: false,
      //弹窗兑换码标题
      inputAdTitle: "",
      //弹窗会员天数
      inputDayNum: "",
      //弹窗使用次数
      inputTimes: "",
      //弹窗有效天数
      inputEffeDayNum: "",
      //弹窗生成数量
      inputCreatedNum: "",
      //弹窗类型
      inputType: "",
      //弹窗备注
      inputNote: "",
      initData: {},
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    //搜索
    searchMethod() {
      this.currentPage1 = 1;
      this.getDataList();
    },
    //添加
    addData() {
      this.inputAdTitle = "";
      this.inputDayNum = "";
      this.inputTimes = "";
      this.inputEffeDayNum = "";
      this.inputCreatedNum = "";
      this.inputNote = "";
      this.inputType = "";
      this.creatCodeDialogVisible = true;
    },
    //导出
    exportFile() {
      this.export2Excel();
    },
    //提交创建兑换码
    async submitCreatCod() {
      if (!this.inputAdTitle) {
        this.$message({
          message: "请填写标题",
          type: "warning"
        });
        return;
      }
      if (!this.inputDayNum) {
        this.$message({
          message: "请填写会员天数",
          type: "warning"
        });
        return;
      }
      if (!this.inputTimes) {
        this.$message({
          message: "请填写剩余次数",
          type: "warning"
        });
        return;
      }
      if (!this.inputCreatedNum) {
        this.$message({
          message: "请填写张数",
          type: "warning"
        });
        return;
      }
      if (!this.inputNote) {
        this.$message({
          message: "请填写倍数",
          type: "warning"
        });
        return;
      }
      // if (!this.inputType) {
      //   this.$message({
      //     message: "请选择类型",
      //     type: "warning"
      //   });
      //   return;
      // }
      let parames = {
        handshake: "",
        data: {
          title: this.inputAdTitle,
          day: this.inputDayNum,
          limit: this.inputTimes,
          expire: this.inputEffeDayNum,
          count: this.inputCreatedNum,
          remark: this.inputNote,
          type: this.inputTimes==1?2:1
        }
      };
      let res = await this.$axios.post(
        this.$api.vipcodeStore,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.creatCodeDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //取消提交
    cancelCreatCod() {
      this.creatCodeDialogVisible = false;
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该行数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          id: id
        }
      };
      let res = await this.$axios.post(
        this.$api.vipcodeDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //数据写入excel
    export2Excel() {
      var that = this;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/excel/Export2Excel"); //这里必须使用绝对路径，使用@/+存放export2Excel的路径
        const tHeader = [
          "标题",
          "兑换码",
          "类型",
          "会员天数",
          "限制次数",
          "有效期",
          "备注",
          "状态",
          "创建时间"
        ]; // 导出的表头名信息
        const filterVal = [
          "title",
          "code",
          "type_text",
          "day",
          "limit",
          "expire",
          "remark",
          "status_text",
          "created_at"
        ]; // 导出的表头字段名，需要导出表格字段名
        const list = that.tableData;
        const data = that.formatJson(filterVal, list);

        export_json_to_excel(tHeader, data, "下载数据excel"); // 导出的表格名称，根据需要自己命名
      });
    },
    //格式转换，直接复制即可
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    //获取初始化会员卡列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            title: this.search1,
            code: this.search2,
            status: this.search3,
            is_expire: this.search4
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.vipcodeLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.status == 0) {
            item.status_text = "已使用";
          } else if (item.status == 1) {
            item.status_text = "未使用";
          }
          if (item.type == 1) {
            item.type_text = "多次使用";
          } else if (item.type == 2) {
            item.type_text = "单次使用";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>